home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-09-10 | 3.3 KB | 115 lines |
- package com.symantec.itools.swing.models;
-
- import com.sun.java.swing.text.AttributeSet;
- import com.sun.java.swing.text.BadLocationException;
- import com.sun.java.swing.text.PlainDocument;
- import com.sun.java.swing.text.Segment;
- import com.sun.java.swing.text.StringContent;
-
- /*
- * Copyright (c) 1998 Symantec Corporation. All Rights Reserved.
- *
- * NumberDocument extends PlainDocument and is supposed to be used as
- * a model in JFC text components where only unsigned numerical values
- * make sense. When this model is used, the user can only enter digits
- * in the JFC text component.
- */
-
- public class UnsignedNumberDocument extends PlainDocument
- {
- private boolean defaultValueEnabled = false;
- private int defaultValue = 0;
-
- /*
- * Inserts a string of content. The string will be inserted only if
- * it contains valid characters. The string will remplace current
- * value if the later is equal to the default value.
- */
- public void insertString (int offset, String string,
- AttributeSet attributeSet)
- throws BadLocationException
- {
- // Check string to be inserted to ensure each character is valid.
- for (int characterIndex = 0; characterIndex < string.length ();
- characterIndex++) {
- if (!Character.isDigit (string.charAt (characterIndex))) {
- // Oups, an invalid character. We won't insert this string.
- return;
- }
- }
- // Check if current value is default value.
- if (defaultValueEnabled && getLength () == 1) {
- if (getText (0, 1).equals (String.valueOf (defaultValue))) {
- super.remove (0, 1);
- super.insertString (0, string, attributeSet);
- } else {
- super.insertString(offset, string, attributeSet);
- }
- } else {
- super.insertString(offset, string, attributeSet);
- }
- }
-
- /*
- * Set the default value returned by <CODE>getText</CODE> and
- * <CODE>getValue</CODE>.
- *
- * @see getText
- * @see getValue
- */
- public void setDefaultValue (int defaultValue)
- {
- if (defaultValueEnabled &&
- String.valueOf (defaultValue).length () != 1) {
- throw new IllegalArgumentException
- ("Default value must have exactly one digit");
- }
- this.defaultValue = defaultValue;
- }
-
- /*
- * Gives the default value returned by <CODE>getText</CODE> and
- * <CODE>getValue</CODE>.
- *
- * @see getText
- * @see getValue
- */
- public int getDefaultValue ()
- {
- return defaultValue;
- }
-
- /*
- * Enable of desable the default value. If the default value is
- * enabled, a user won't be able to remove all digits and thus the
- * value will at least contain the default value.
- */
- public void setDefaultValueEnabled (boolean defaultValueEnabled)
- {
- this.defaultValueEnabled = defaultValueEnabled;
- }
-
- /*
- * Returns true if the default value is enabled, false otherwise.
- *
- * @see setDefaultValueEnabled
- */
- public boolean isDefaultValueEnabled ()
- {
- return defaultValueEnabled;
- }
-
- /*
- * Override superclass <CODE>remove</CODE> method to prevent
- * removing all digits if default value is enabled.
- */
- public void remove (int offset, int length)
- throws BadLocationException
- {
- super.remove (offset, length);
- if (defaultValueEnabled && getLength () == 0) {
- insertString (0, String.valueOf (defaultValue), null);
- }
- }
- }
-